//
//  FSBaseTopTableViewCell.m
//  FSScrollViewNestTableViewDemo
//
//  Created by huim on 2017/5/23.
//  Copyright © 2017年 fengshun. All rights reserved.
//

#import "HHBaseTopTableViewCell.h"
#import "StackBtnCollectionViewCell.h"
#import "SupportCategoryModel.h"
#import "SmallButtonViewCell.h"
#import "ButtonReusaViewModel.h"

@interface HHBaseTopTableViewCell ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *buttonCollectionView;


@end
@implementation HHBaseTopTableViewCell


- (void)setOneArray:(NSArray *)oneArray {
    _oneArray = oneArray;
    [self.buttonCollectionView reloadData];
}

- (void)setTwoArray:(NSArray *)twoArray {
    _twoArray = twoArray;
    [self.buttonCollectionView reloadData];
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = Colorf2f2f2;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        UIView *bgview = [[UIView alloc] init];
        bgview.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:bgview];
        [bgview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.mas_equalTo(Margin);
            make.bottom.right.mas_equalTo(-Margin);
        }];
        MKViewBorderRadius( bgview, 10, 0, [UIColor whiteColor]);

        [bgview addSubview:self.buttonCollectionView];
        [self.buttonCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(0);
            make.top.mas_equalTo(Margin);
        }];
        
    }
    return self;
}



- (UICollectionView *)buttonCollectionView {
if (!_buttonCollectionView) {
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    _buttonCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
    _buttonCollectionView.backgroundColor = WhiteColor;
    _buttonCollectionView.delegate = self;
    _buttonCollectionView.dataSource = self;
    _buttonCollectionView.scrollEnabled = NO;
    _buttonCollectionView.showsVerticalScrollIndicator = NO;
    _buttonCollectionView.showsHorizontalScrollIndicator = NO;
    [_buttonCollectionView registerClass:[StackBtnCollectionViewCell class] forCellWithReuseIdentifier:@"StackBtnCollectionViewCell"];
    [_buttonCollectionView registerClass:[SmallButtonViewCell class] forCellWithReuseIdentifier:@"SmallButtonViewCell"];
    
    if (@available (iOS 11.0, *)) {
        _buttonCollectionView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    }
    return _buttonCollectionView;
}

#pragma mark -collectionV  delegate\datasource

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat width = (WIDTH - 2*Margin) / 4.0;
    CGFloat height = jisuan(130);
    return indexPath.section == 0 ? CGSizeMake(width, height) : CGSizeMake((WIDTH - 2*Margin) / 5.0, (WIDTH - 2*Margin) / 5.0);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        self.topViewCollectionClick(self.oneArray[indexPath.row], indexPath.section);
    } else {
        self.topViewCollectionClick(self.twoArray[indexPath.row], indexPath.section);
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 2;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return section == 0 ? self.oneArray.count : self.twoArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        StackBtnCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StackBtnCollectionViewCell" forIndexPath:indexPath];
        cell.categoryModel = self.oneArray[indexPath.item];
        return cell;
    } else {
        SmallButtonViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmallButtonViewCell" forIndexPath:indexPath];
        cell.model = self.twoArray[indexPath.item];
        return cell;
    }
  
}

@end
